-- 铁血战士冈布奥建筑

function initPredatorSlime(self, buildingInfo, dbase, pos)
    if type(dbase) ~= "table" or type(dbase["need_property"]) ~= "number" then
        -- 没要求，配置错误
        error("获取不到铁血战士冈布奥物品配置！");
        return;
    end

    -- 关注玩家卸下装备
    EventMgr.removeAll("UIGrid/SlimeEvent/PredatorSlime");
    EventMgr.register("UIGrid/SlimeEvent/PredatorSlime", event.UNEQUIP, function(params)
        if params["classId"] ~= dbase["need_property"] then
            return;
        end

        if self.iconPath and self.icon then
            self.icon:loadTexture(self.iconPath);
        end
    end);

    -- 关注玩家装上装备
    EventMgr.register("UIGrid/SlimeEvent/PredatorSlime", event.EQUIP, function(classId)
        if classId ~= dbase["need_property"] then
            return;
        end

        if self.iconChangePath and self.icon then
            self.icon:loadTexture(self.iconChangePath);
        end
    end);

    -- 雇佣铁血战士
    local function okFun()
        -- 金币足够否
        local bonusArgs = SlimeEventM.query(105, "bonus_args")
        local cost = bonusArgs["cost"][1];
        if cost[1] ~= 2 or cost[2] ~= "money" then
            -- 配置错误
            error("铁血战士扣除配置错误！");
            return;
        end

        -- 判断玩家金钱是否足够
        if ME.user.dbase:query(cost[2], 0) < cost[3] then
            -- 关闭建筑界面
             UIMgr.getCurrentScene():removeFormByName("UIBuildingView2");
            -- 显示商城购买金币界面
            showBuyMoneyTip();
            return;
        end

        -- 尝试尝试触发
        DungeonActionM.go("slime_event", pos, 105);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    -- 获得铁血战士奖励碎片
    local function okFun2()
        -- 尝试触发事件
        DungeonActionM.go("slime_event", pos, 106);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    local petId = SlimeEventM.query(105, "dbase")["pet_id"];
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 格子失效了
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            if EquipM.isEquiped(dbase["need_property"]) then
                -- 装备异族头盔
                -- 有铁血战士显示购买碎片
                if not petId then
                    error("SLIME_EVENT_105 (pet_id)没有配置！");
                    return;
                end

                if PetM.getMyPet(petId) then
                    self:openBuildingView(okFun2, 0, buildingInfo["dialog2"], getLocStr("btn_text_thanks"), nil, getHeroIconPath(petId));
                else
                    -- 没有铁血战士，显示雇佣铁血战士
                    self:openBuildingView2(okFun, nil, buildingInfo["view_text"], buildingInfo["view_text2"],
                        buildingInfo["dialog1"], getHeroIconPath(petId), true);
                end
                AudioM.playFx("slime_alchemist_app");
            else
                -- 没装备点击没反应
            end
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:addTouchEventListener(onClicked);
    self.iconChangePath = getHeroIconPath(petId);
    self.needAnimation =  true;

    -- 如果装备了头盔，直接显示出来
    if EquipM.isEquiped(dbase["need_property"]) then
        self.icon:loadTexture(self.iconChangePath);
    end
end
